package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.domain.ZhwyPark;
import com.ruoyi.mapper.ZhwyParkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ZhwyDeviceMapper;
import com.ruoyi.domain.ZhwyDevice;
import com.ruoyi.service.IZhwyDeviceService;

/**
 * 道闸设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-08
 */
@Service
public class ZhwyDeviceServiceImpl implements IZhwyDeviceService 
{
    @Autowired
    private ZhwyDeviceMapper zhwyDeviceMapper;
    @Autowired
    private ZhwyParkMapper zhwyParkMapper;

    /**
     * 查询道闸设备
     * 
     * @param id 道闸设备主键
     * @return 道闸设备
     */
    @Override
    public ZhwyDevice selectZhwyDeviceById(Long id)
    {
        return zhwyDeviceMapper.selectZhwyDeviceById(id);
    }

    /**
     * 查询道闸设备列表
     * 
     * @param zhwyDevice 道闸设备
     * @return 道闸设备
     */
    @Override
    public List<ZhwyDevice> selectZhwyDeviceList(ZhwyDevice zhwyDevice)
    {
        List<ZhwyDevice> list = zhwyDeviceMapper.selectZhwyDeviceList(zhwyDevice);
        for(ZhwyDevice zd : list){
            ZhwyPark park = zhwyParkMapper.selectZhwyParkById(zd.getParkId());
            zd.setParkName(park.getName());
        }
        return list;
    }

    /**
     * 新增道闸设备
     * 
     * @param zhwyDevice 道闸设备
     * @return 结果
     */
    @Override
    public int insertZhwyDevice(ZhwyDevice zhwyDevice)
    {

        List<ZhwyDevice> list=zhwyDeviceMapper.listAll();
        System.out.println("list为"+list.toString());
        int count=0;
        for(ZhwyDevice device : list){
            if(device.getName().equals(zhwyDevice.getName()) &&(device.getState()==1)){
                System.out.println("已存在!");
                break;
            }
            count++;
        }
        if(count==list.size()){
            return zhwyDeviceMapper.insertZhwyDevice(zhwyDevice);
        }
        return 0;
    }

    /**
     * 修改道闸设备
     * 
     * @param zhwyDevice 道闸设备
     * @return 结果
     */
    @Override
    public int updateZhwyDevice(ZhwyDevice zhwyDevice)
    {
        return zhwyDeviceMapper.updateZhwyDevice(zhwyDevice);
    }

    /**
     * 批量删除道闸设备
     * 
     * @param ids 需要删除的道闸设备主键
     * @return 结果
     */
    @Override
    public int deleteZhwyDeviceByIds(Long[] ids)
    {
        return zhwyDeviceMapper.deleteZhwyDeviceByIds(ids);
    }

    /**
     * 删除道闸设备信息
     * 
     * @param id 道闸设备主键
     * @return 结果
     */
    @Override
    public int deleteZhwyDeviceById(Long id)
    {
        return zhwyDeviceMapper.deleteZhwyDeviceById(id);
    }
}
